elasticsearch 基本概念

前言

本章主要介绍Elasticsearch的一些基本概念,以及它与关系型数据之间的类比。比如索引(indices),类型(types),文档(documents),字段(fields)等。Elasticsearch 使用JSON作为文档的存储格式。JSON序列化被大多数编程语言所支持,并且已经成为NoSQL领域的标准格式。 它简单、简洁、易于阅读。

类比

索引(Index): 类似于关系数据库中的一个数据库Database,是一个存储关系型文档的地方,ES集群包含多个索引集(indices);

类型(Type): 类似于关系数据库中的表Table,包含多个文档集(documents);

文档(Document): 类似于关系数据库中的表记录行Row,包含多个字段(fields);

字段(Field): 类似于关系数据库中表记录的每一列Column;

举例

image

客户关系管理:Index,索引名称命名为megacorp

客户类型:Type,类型名称命名为customer

添加文档记录

请求:

curl -H "Content-type: application/json" -X POST -d '{"name":"lipanpan", "age":26, "phoneNo":"13243744257", "address":"广东深圳"}' 'http://127.0.0.1:9200/megacorp/customer/1'

响应:

{"_index":"megacorp","_type":"customer","_id":"1","_version":1,
"result":"created","_shards":{"total":2,"successful":1,"failed":0}

查询文档记录

请求:

curl -X GET 'http://127.0.0.1:9200/megacorp/customer/1'

响应:

{"_index":"megacorp","_type":"customer","_id":"1","_version":1,"found":true,
"_source":{"name":"lipanpan", "age":26, "phoneNo":"13243744257", "address":"广东深圳"}}

更新文档记录

请求:

curl -H "Content-type: application/json" -X PUT -d '{"name":"lipanpan1", "age":27, "phoneNo":"13243744257", "address":"广东深圳"}' 'http://127.0.0.1:9200/megacorp/customer/1'

响应:

{"_index":"megacorp","_type":"customer","_id":"1","_version":3,"result":"updated",
"_shards":{"total":2,"successful":1,"failed":0},"created":false}

删除文档记录

请求:

curl -X DELETE 'http://127.0.0.1:9200/megacorp/customer/1'

响应:

{"found":true,"_index":"megacorp","_type":"customer","_id":"1","_version":4,
"result":"deleted","_shards":{"total":2,"successful":1,"failed":0}}

参考链接

  1. https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_indexing_employee_documents.html